package pro1.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pro1.dao.AdminCharacterMapper;
import pro1.dao.UserInfoMapper;
import pro1.dao.UserMapper;
import pro1.model.AdminCharacter;
import pro1.model.ResponseBody;
import pro1.model.User;
import pro1.model.UserInfo;
import pro1.service.UserService;
import pro1.util.MD5Util;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	UserMapper userMapper;

	@Autowired
	UserInfoMapper userInfoMapper;

	@Autowired
	AdminCharacterMapper adminCharacterMapper;

	@Override
	public ResponseBody allUsers(String userName, String name, Integer sex, Integer dep, Integer page, Integer num) {
		page = (page - 1) * num;
		return new ResponseBody(1, "成功", userMapper.selectAll(userName, name, sex, dep, page, num));
	}

	@Override
	public ResponseBody checkUserName(String userName) {

		return new ResponseBody(1, "成功", userMapper.selectNumByUserName(userName));
	}

	@Override
	@Transactional
	public ResponseBody addOrUpdateUser(Integer userId, String userName, String name, String birth, Integer sex,
			Integer dep, String address) {
		ResponseBody response = new ResponseBody(1, "成功");
		if (userId == null) {
			User user = new User();
			userId = userMapper.selectMaxId() + 1;
			user.setId(userId);
			user.setUsername(userName);
			user.setPassword(MD5Util.MD5Encode("123456", null));
			user.setRole(0);
			UserInfo userInfo = new UserInfo();
			userInfo.setId(userId);
			userInfo.setName(name);
			userInfo.setSex(sex);
			userInfo.setBirth(birth);
			userInfo.setDepartment(dep);
			userInfo.setAddress(address);
			userMapper.insertSelective(user);
			userInfoMapper.insertSelective(userInfo);
		} else {
			User user = userMapper.selectByPrimaryKey(userId);
			if (user == null) {
				response = new ResponseBody(0, "用户不存在");
			} else {
				user.setUsername(userName == null ? user.getUsername() : userName);
				UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
				userInfo.setName(name == null ? userInfo.getName() : name);
				userInfo.setSex(sex == null ? userInfo.getSex() : sex);
				userInfo.setBirth(birth == null ? userInfo.getBirth() : birth);
				userInfo.setDepartment(dep == null ? userInfo.getDepartment() : dep);
				userInfo.setAddress(address == null ? userInfo.getAddress() : address);
				userMapper.updateByPrimaryKeySelective(user);
				userInfoMapper.updateByPrimaryKeySelective(userInfo);
			}
		}
		return response;
	}

	@Override
	@Transactional
	public ResponseBody deleteUser(Integer userId) {
		userMapper.deleteByPrimaryKey(userId);
		userInfoMapper.deleteByPrimaryKey(userId);
		return new ResponseBody(1, "成功");
	}

	@Override
	public ResponseBody updatePwd(Integer userId, String pwd) {
		ResponseBody response = new ResponseBody(1, "成功");
		User user = userMapper.selectByPrimaryKey(userId);
		if (user == null) {
			response = new ResponseBody(0, "用户不存在");
		} else {
			user.setPassword(MD5Util.MD5Encode(pwd, null));
			userMapper.updateByPrimaryKeySelective(user);
		}
		return response;
	}

	@Override
	public ResponseBody allAdmins() {

		return new ResponseBody(1, "成功", userMapper.selectAllAdmin());
	}

	@Override
	@Transactional
	public ResponseBody addOrUpdateAdmin(Integer userId, String userName, String pwd, Integer characterId) {
		ResponseBody response = new ResponseBody(1, "成功");
		if (userId == null) {
			User user = new User();
			userId = userMapper.selectMaxId() + 1;
			user.setId(userId);
			user.setUsername(userName);
			user.setPassword(MD5Util.MD5Encode(pwd, null));
			user.setRole(1);
			userMapper.insertSelective(user);
			if (characterId != null) {
				AdminCharacter adminCharacter = new AdminCharacter();
				adminCharacter.setId(userId);
				adminCharacter.setCharacterid(characterId);
				adminCharacterMapper.insertSelective(adminCharacter);
			}
		} else {
			User user = userMapper.selectByPrimaryKey(userId);
			if (user == null) {
				response = new ResponseBody(0, "用户不存在");
			} else {
				user.setUsername(userName);
				AdminCharacter adminCharacter = adminCharacterMapper.selectByPrimaryKey(userId);
				if (adminCharacter == null) {
					adminCharacter = new AdminCharacter();
					adminCharacter.setId(user.getId());
					adminCharacter.setCharacterid(characterId);
					adminCharacterMapper.insertSelective(adminCharacter);
				} else {
					adminCharacter.setCharacterid(characterId);
					adminCharacterMapper.updateByPrimaryKeySelective(adminCharacter);
				}
				userMapper.updateByPrimaryKeySelective(user);
			}
		}
		return response;
	}

	@Override
	public ResponseBody updatePwd(Integer userId, String oldPwd, String pwd) {
		ResponseBody response = new ResponseBody(1, "成功");
		User user = userMapper.selectByPrimaryKey(userId);
		if (user == null) {
			response = new ResponseBody(0, "用户不存在");
		} else {
			if (!MD5Util.MD5Encode(oldPwd, null).equals(user.getPassword())) {
				response = new ResponseBody(0, "原密码错误");
			} else {
				user.setPassword(MD5Util.MD5Encode(pwd, null));
				userMapper.updateByPrimaryKeySelective(user);
			}
		}
		return response;
	}

}
